package com.gitee.feizns.v8;

import com.alibaba.fastjson.JSONObject;
import com.gitee.feizns.v8.json.ReadConfig;
import com.gitee.feizns.v8.rander.Utils;
import com.gitee.feizns.v8.utils.IOUtils;
import lombok.SneakyThrows;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.runtime.RuntimeConstants;
import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;

import java.io.File;
import java.io.FileInputStream;
import java.io.StringWriter;

/**
 * @author feizns
 * @since 2019/8/13
 */
public class Main {

    @SneakyThrows
    public static void main(String[] args) {

//        ReadConfig config = JSONObject.parseObject(IOUtils.readerToStr(Main.class.getResourceAsStream("/v8/meta.json")), ReadConfig.class);
        ReadConfig config = JSONObject.parseObject(IOUtils.readerToStr(new FileInputStream("C:\\Users\\Administrator\\Desktop\\test\\gg\\meta.json")), ReadConfig.class);

        VelocityEngine engine = new VelocityEngine();

        engine.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
        engine.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
        engine.setProperty(Velocity.INPUT_ENCODING, "UTF-8");
        engine.setProperty(Velocity.OUTPUT_ENCODING, "UTF-8");
        engine.setProperty(Velocity.ENCODING_DEFAULT, "UTF-8");

        engine.init();

        Template template = engine.getTemplate("/v8/Pojo.vm");

        VelocityContext vc = new VelocityContext();
        vc.put("utils", new Utils());
        vc.put("table", config.getTables().get(0));
        vc.put("tables", config.getTables());
        vc.put("author", config.getAuthor());
        vc.put("date", config.getCurrentDate());
        vc.put("packageConfig", config.getPackageConfig());
        vc.put("dataConfig", config.getDataConfig());
        StringWriter sw = new StringWriter();

        template.merge(vc, sw);

        System.out.println(sw);

        IOUtils.write(sw.toString(), new File("C:\\Users\\Administrator\\Desktop\\Hello.java"));
    }

}
